/*
 * @lc app=leetcode id=563 lang=cpp
 *
 * [563] Binary Tree Tilt
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int findTilt(TreeNode* root) {

        int tilt = 0, sum = 0;
        findTilt(root, tilt, sum);
        return tilt;
    }

    void findTilt(TreeNode* root, int& tilt, int& sum) {

        if (!root) {
            tilt = 0;
            sum = 0;
            return;
        }

        int lt = 0, rt = 0;
        int ls = 0, rs = 0;

        findTilt(root->left, lt, ls);
        findTilt(root->right, rt, rs);

        tilt = std::abs(ls - rs) + lt + rt;
        sum = ls + rs + root->val;
    }
};

